// ================================================================================
// 		File: PartyJoinChildFlow.cs
// 		Desc: 
//  
// 		Called by:   
//               
// 		Auth: chenwenzhi（--）
// 		Date: 2015-12-28 23:59
// ================================================================================
// 		Change History
// ================================================================================
// 		Date:		Author:				Description:
// 		--------	--------			-------------------
//    
// ================================================================================
//
// ================================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace ZHDJ.Data.Models.JoinParty
{
    /// <summary>
    /// 
    /// </summary>   
    public class PartyJoinChildFlow
    {
        #region 属性
        /// <summary>
        /// 
        /// </summary>
        [Key]
        public string ChildFlowID { get; set; }
        /// <summary>
        /// StandID
        /// </summary>
        [StringLength(128)]
        public string StandID { get; set; }

        /// <summary>
        /// ApplyBranchID
        /// </summary>
        [StringLength(128)]
        public string ApplyBranchID { get; set; }

        /// <summary>
        /// StepID
        /// </summary>
        [StringLength(128)]
        public string StepID { get; set; }

        /// <summary>
        /// HandleUserID
        /// </summary>
        [StringLength(128)]
        public string HandleUserID { get; set; }

        /// <summary>
        /// HandleUserName
        /// </summary>
        [StringLength(128)]
        public string HandleUserName { get; set; }

        /// <summary>
        /// ToDoHandleUserID
        /// </summary>
        [StringLength(128)]
        public string ToDoHandleUserID { get; set; }

        /// <summary>
        /// ToDoHandleUserName
        /// </summary>
        [StringLength(128)]
        public string ToDoHandleUserName { get; set; }

        /// <summary>
        /// ToDoBranchID
        /// </summary>
        [StringLength(128)]
        public string ToDoBranchID { get; set; }

        /// <summary>
        /// Status
        /// </summary>
        public int Status { get; set; }

        /// <summary>
        /// Remark
        /// </summary>
        public string Remark { get; set; }

        /// <summary>
        /// HandleTime
        /// </summary>
        public DateTime? HandleTime { get; set; }

        /// <summary>
        /// OrderNo
        /// </summary>
        public int OrderNo { get; set; }

        /// <summary>
        /// CreateTime
        /// </summary>
        public DateTime? CreateTime { get; set; }

        #endregion

        #region 处理流程
        /// <summary>
        /// 处理流程
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static bool HandleFlow(PartyJoinChildFlow mode)
        {
            StringBuilder sbsql = new StringBuilder();
            sbsql.AppendFormat(@" UPDATE [tblPartyJoinChildFlow]
                                        SET [HandleUserID] ='{0}'
	                                        ,[HandleUserName] ='{1}'
	                                        ,[Status] ={2}
	                                        ,[HandleTime] ='{3}'
                                        where StandID='{4}' and [Status]=0", mode.HandleUserID, mode.HandleUserName, mode.Status, mode.HandleTime, mode.StandID);
            return MySqlHelper.ExecuteNonQuery(sbsql.ToString()) > 0;

        }
        #endregion

        #region 获取子流程条数  （若可以加入除支部外其他组织，该方法将失效）
        /// <summary>
        /// 获取当前流程中子流程条数
        /// </summary>
        /// <param name="StandID"></param>
        /// <returns></returns>
        public static int GetChildFlowCountByStandID(string StandID)
        {
            StringBuilder sbsql = new StringBuilder();
            sbsql.AppendFormat(@" select * from tblPartyJoinChildFlow where StandID='{0}'", StandID);
            DataSet ds = MySqlHelper.ExecuteDataSet(sbsql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
                return ds.Tables[0].Rows.Count;
            else return 0;
        }
        #endregion

        #region 撤回上一步骤
        /// <summary>
        /// 撤回上一步骤
        /// </summary>
        /// <param name="StandID"></param>
        /// <param name="AppllicationID"></param>
        public static void ReLastStep(string StandID, string AppllicationID, out string TipMessage)
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                TipMessage = "";
                //当前环节是否最新环节
                StringBuilder sbSql = new StringBuilder();
                sbSql.AppendFormat(@"select Top 1 StandID from tblPartyJoinLinkExample 
where  AppllicationID ='{0}'
order by OrderNo desc", AppllicationID);
                cmd.Parameters.Add("@AppllicationID", SqlDbType.NVarChar).Value = AppllicationID;
                cmd.CommandText = sbSql.ToString();
                string TopStandID = Convert.ToString(MySqlHelper.ExecuteScalar(sbSql.ToString()));
                //当前环节是最新环节
                if (TopStandID == StandID)
                {
                    sbSql = new StringBuilder();
                    sbSql.AppendFormat(@" delete tblPartyJoinLinkExample where ID='{0}'
delete tblPartyJoinChildFlow where StandID='{0}'

update tblPartyJoinLinkExample set Status='0' where StandID in(
select Top 1 StandID from tblPartyJoinLinkExample 
where  AppllicationID ='{1}'
order by OrderNo desc)

select Top 1 StandID from tblPartyJoinLinkExample 
where  AppllicationID ='{1}'
order by OrderNo desc", StandID, AppllicationID);
                    //string UPStandID = Convert.ToString(MySqlHelper.ExecuteScalar(sbSql.ToString()));

                    //清除待办
                    //SXT.Party.Utility.PartyTask.TaskDone(StandID, SXT.Utility.SSOClient.CurrentUser.LoginID);
                    ////还原上一步待办
                    //SXT.Party.Utility.PartyTask.ReductionTaskDone(UPStandID, SXT.Utility.SSOClient.CurrentUser.LoginID);

                    TipMessage = "撤消成功！";
                }
                else
                {
                    TipMessage = "撤消失败，该流程已被审核！";
                }
            }
        }

        #endregion

        #region 根据组织ID获取所有待处理信息
        /// <summary>
        /// 根据组织ID获取所有待处理信息
        /// </summary>
        /// <param name="BranchID"></param>
        /// <param name="CurrentStepID"></param>
        /// <returns></returns>
        public static DataSet ChildBranchLoginStantID(string applyBranchID, string stepID)
        {
            StringBuilder sbsql = new StringBuilder();
            sbsql.AppendFormat(@" select * from tblPartyJoinChildFlow cf
                                where (Status is null or Status='0') and applyBranchID='{0}'  and stepID='{1}'", applyBranchID, stepID);
            DataSet ds = MySqlHelper.ExecuteDataSet(sbsql.ToString());
            return ds;

        }
        #endregion

        #region 根据standid获取最新的childFlowID
        /// <summary>
        /// 根据standid获取最新的childFlowID
        /// </summary>
        /// <param name="standID"></param>
        /// <returns></returns>
        public static string GetChildFlowID(string standID)
        {
            string sql = "SELECT ChildFlowID FROM tblPartyJoinChildFlow WHERE StandID='" + standID + "' ORDER BY OrderNo DESC LIMIT 1";
            object o = MySqlHelper.ExecuteScalar(sql);

            return Convert.ToString(o);

        }
        #endregion
    }
}
